 using System.Collections.Generic;
using System.Windows.Controls;
using Me.Amon.Open;
using Me.Amon.Pcs.C;
using Me.Amon.Pcs.M;

namespace Me.Amon.Pcs.V
{
    /// <summary>
    /// UcNtp.xaml 的交互逻辑
    /// </summary>
    public partial class TabPage : UserControl
    {
        private WPcs _WPcs;
        private OAuthPcs _CsEngine;
        private PcEngine _PcEngine;

        #region 构造函数
        public TabPage()
        {
            InitializeComponent();
        }

        public TabPage(WPcs wPcs, OAuthPcs engine)
        {
            _WPcs = wPcs;
            _CsEngine = engine;
            //_PcEngine = new NativeEngine();

            InitializeComponent();
        }
        #endregion

        public void Init()
        {
            TreeViewItem root = new TreeViewItem();
            root.Header = new UcTreeNode { Text = "收藏" };
            root.Tag = "<favorites>";
            TvGuid.Items.Add(root);

            root = new TreeViewItem();
            root.Header = new UcTreeNode { Text = "公共" };
            root.Tag = "<libraries>";
            TvGuid.Items.Add(root);

            TreeViewItem node = new TreeViewItem();
            node.Header = new UcTreeNode { Text = "文档" };
            node.Tag = CPcs.PATH_DOCUMENTS;
            root.Items.Add(node);

            node = new TreeViewItem();
            node.Header = new UcTreeNode { Text = "图片" };
            node.Tag = CPcs.PATH_PICTURES;
            root.Items.Add(node);

            node = new TreeViewItem();
            node.Header = new UcTreeNode { Text = "音乐" };
            node.Tag = CPcs.PATH_AUDIOS;
            root.Items.Add(node);

            node = new TreeViewItem();
            node.Header = new UcTreeNode { Text = "视频" };
            node.Tag = CPcs.PATH_VIDEOS;
            root.Items.Add(node);

            root = new TreeViewItem();
            root.Header = new UcTreeNode { Text = "所有" };
            root.Tag = "<storage>";
            TvGuid.Items.Add(root);

            root = new TreeViewItem();
            root.Header = new UcTreeNode { Text = "分享" };
            root.Tag = CPcs.PATH_SHARES;
            TvGuid.Items.Add(root);

            root = new TreeViewItem();
            root.Header = new UcTreeNode { Text = "回收站" };
            root.Tag = CPcs.PATH_RECYCLE;
            TvGuid.Items.Add(root);
        }

        #region 公共函数
        public void CutMeta()
        {
            var list = LvMeta.SelectedItems;
            if (list.Count < 1)
            {
                return;
            }

            //var item = list[0] as ListViewItem;
            //if (item == null)
            //{
            //    return;
            //}

            var meta = list[0] as CsMeta;
            if (meta == null)
            {
                return;
            }

            LvMeta.Items.Remove(list[0]);
            _WPcs.SelectedMeta = meta;
            _WPcs.Operation = EPcs.Cut;
        }

        public void CopyMeta()
        {
            var list = LvMeta.SelectedItems;
            if (list.Count < 1)
            {
                return;
            }

            //var item = list[0] as ListViewItem;
            //if (item == null)
            //{
            //    return;
            //}

            var meta = list[0] as CsMeta;
            if (meta == null)
            {
                return;
            }

            _WPcs.SelectedMeta = meta;
            _WPcs.Operation = EPcs.Copy;
        }

        public void PasteMeta()
        {
            if (_WPcs.Operation == EPcs.Cut)
            {
                CsMeta meta = _WPcs.SelectedMeta;

                if (_CsEngine.Parent(meta.Path) == _CsEngine.Path)
                {
                    return;
                }

                string path = _CsEngine.Combine(_CsEngine.Path, meta.Name);
                _CsEngine.Moveto(meta.Path, path);
                meta.Path = path;

                var item = new ListViewItem();
                item.Content = new UcViewItem { Text = meta.Name, Icon = _WPcs.GetIcon(meta.Name) };
                LvMeta.Items.Add(item);
                item.IsSelected = true;
                return;
            }
            if (_WPcs.Operation == EPcs.Copy)
            {
                CsMeta meta = _WPcs.SelectedMeta;

                if (_CsEngine.Parent(meta.Path) == _CsEngine.Path)
                {
                    meta.Name = "复件 " + _WPcs.SelectedMeta.Name;
                }

                string path = _CsEngine.Combine(_CsEngine.Path, meta.Name);
                _CsEngine.Copyto(meta.Path, path);
                meta.Path = path;

                var item = new ListViewItem();
                item.Content = new UcViewItem { Text = meta.Name, Icon = _WPcs.GetIcon(meta.Name) };
                LvMeta.Items.Add(item);
                item.IsSelected = true;
                return;
            }
        }
        #endregion

        #region 事件处理
        private void TvGuid_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
        {
            var node = TvGuid.SelectedItem as TreeViewItem;
            if (node == null)
            {
                return;
            }

            string tag = node.Tag as string;
            if (string.IsNullOrEmpty(tag))
            {
                return;
            }

            ShowMeta(_CsEngine.ListMeta(tag));
        }
        #endregion

        #region 视图更新
        private void ShowMeta(List<CsMeta> metas)
        {
            LvMeta.Items.Clear();

            foreach (CsMeta meta in metas)
            {
                meta.Icon = _WPcs.GetIcon(meta.Name);
                LvMeta.Items.Add(meta);
            }
        }
        #endregion
    }
}
